﻿/*
 * Copyright 2014 Francois Karman
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;

namespace Quartz.WebManager.Model
{
    /// <summary>
    /// Tests the <see cref="PropertyValidator"/> class.
    /// </summary>
    [TestClass]
    public class PropertyValidatorTest
    {
        /// <summary>
        /// Tests the <see cref="PropertyValidator.Instance"/> property.
        /// </summary>
        [TestMethod]
        public void Instance()
        {
            Assert.IsNotNull(PropertyValidator.Instance);
        }

        /// <summary>
        /// Tests the <see cref="PropertyValidator.AddValidator"/> method.
        /// </summary>
        [TestMethod]
        public void AddValidator()
        {
            string tested = null;
            PropertyValidator.Instance.AddValidator("test type", value => { tested = value; return "test error"; });

            JobDefinition definition = new JobDefinition();
            definition.Properties.Add(new DataPropertyInfo() { Name = "test prop", Type = "test type" });

            JobDetail detail = new JobDetail();
            detail.Definition = definition;
            detail.Properties["test prop"] = "test value";

            Assert.IsNull(tested);
            var results = PropertyValidator.Instance.Validate(detail);
            Assert.AreEqual("test value", tested);
            Assert.IsNotNull(results);
            Assert.AreEqual(1, results.Count);
            Assert.IsTrue(results.ContainsKey("test prop"));
            Assert.AreEqual("test error", results["test prop"]);
        }

        /// <summary>
        /// Tests the <see cref="PropertyValidator.Validate"/> method.
        /// </summary>
        [TestMethod]
        public void Validate()
        {
            JobDefinition definition = new JobDefinition();
            definition.Properties.Add(new DataPropertyInfo() { Name = "int", Type = "int" });
            definition.Properties.Add(new DataPropertyInfo() { Name = "float", Type = "float" });
            definition.Properties.Add(new DataPropertyInfo() { Name = "string", Type = "string", Required = true });
            definition.Properties.Add(new DataPropertyInfo() { Name = "url", Type = "url" });

            JobDetail detail = new JobDetail();
            detail.Definition = definition;
            IDictionary<string, string> results;

            // Test with valid values
            detail.Properties["int"] = "10";
            detail.Properties["float"] = "20,03";
            detail.Properties["string"] = "test string";
            detail.Properties["url"] = "https://quartzwebmanager.codeplex.com";
            results = PropertyValidator.Instance.Validate(detail);
            Assert.IsNotNull(results);
            Assert.AreEqual(0, results.Count);

            // Test with invalid values
            detail.Properties["int"] = "test";
            detail.Properties["float"] = "wrong";
            detail.Properties["string"] = "";
            detail.Properties["url"] = "https:\\quartzwebmanager.codeplex.com";
            results = PropertyValidator.Instance.Validate(detail);
            Assert.IsNotNull(results);
            Assert.AreEqual(4, results.Count);
            Assert.IsNotNull(results["int"]);
            Assert.IsNotNull(results["float"]);
            Assert.IsNotNull(results["string"]);
            Assert.IsNotNull(results["url"]);
        }
    }
}
